package ads.pin.pinmon.scenes;

import ads.pin.pinmon.entity.Creature;
import ads.pin.pinmon.entity.Enemy;
import ads.pin.pinmon.entity.Player;
import ads.pin.pinmon.extras.Timer;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class GameScene extends Scene {

	private Player m_player;
	private Enemy m_enemy;
	private Creature m_playerCreature;
	private Creature m_enemyCreature;
	private int m_playerCreatureIndex;
	private int m_enemyCreatureIndex;
	private int m_currentPoints;
	private int m_totalPoints;
	private Timer m_turnTimer;

	public GameScene(Game game, Player player) {
		this.m_game = game;
		this.m_spriteBatch = new SpriteBatch();
		m_player = player;
		m_enemyCreatureIndex = 0;
		m_playerCreatureIndex = 0;
		m_turnTimer = new Timer(5);
		m_currentPoints = 0;
		m_totalPoints = player.getTotalPoints();

		init();
	}

	public void init() {
		m_turnTimer.start();
	}

	@Override
	public void update(float deltaTime) {

		// Atualiza as criaturas ativas no campo de batalha.
		if (m_playerCreature == null || !m_playerCreature.isAlive())
			m_playerCreature = m_player.getCreatures().get(
					m_playerCreatureIndex);

		if (m_enemyCreature == null || !m_enemyCreature.isAlive())
			m_enemyCreature = m_enemy.getCreatures().get(m_playerCreatureIndex);

		if (m_turnTimer.hasCompleted())
			pollSkill();

		if (!m_playerCreature.isAlive()) {

			// Checa se o jogador ainda possui criaturas vivas.
			if (checkCreatureRemaining(m_playerCreatureIndex, m_player
					.getCreatures().size()))
				++m_playerCreatureIndex;

			else {
				m_player.setTotalPoints(m_totalPoints);
				// Chama a tela de final do jogo.
				m_game.setScreen(new GameOverScene(m_game, m_player));
			}
		}

		if (!m_enemyCreature.isAlive()) {

			m_currentPoints += 10;
			m_totalPoints += m_currentPoints;
			m_player.setRoundPoints(m_currentPoints);

			// Checa se o oponente ainda possui criaturas vivas.
			if (checkCreatureRemaining(m_enemyCreatureIndex, m_enemy
					.getCreatures().size()))
				++m_enemyCreatureIndex;

			else {
				m_player.setTotalPoints(m_totalPoints);
				// Chama a tela de final do jogo.
				m_game.setScreen(new GameOverScene(m_game, m_player));
			}
		}
	}

	/**
	 * Checa qual skill foi usada pelo jogador e a ativa.
	 */
	private void pollSkill() {

		// Verifica a tecla pressionada e reage de acordo.
		if (Gdx.input.isKeyPressed(Input.Keys.Q)) {
			m_playerCreature.attack(m_enemyCreature, m_playerCreature
					.getSkills().get(0));
			m_turnTimer.start();

		}

		if (Gdx.input.isKeyPressed(Input.Keys.W)) {
			m_playerCreature.attack(m_enemyCreature, m_playerCreature
					.getSkills().get(1));
			m_turnTimer.start();

		}

		if (Gdx.input.isKeyPressed(Input.Keys.E)) {
			m_playerCreature.attack(m_enemyCreature, m_playerCreature
					.getSkills().get(2));
			m_turnTimer.start();

		}

		if (Gdx.input.isKeyPressed(Input.Keys.R)) {
			m_playerCreature.attack(m_enemyCreature, m_playerCreature
					.getSkills().get(3));
			m_turnTimer.start();
		}

	}

	/**
	 * Checa se ainda resta alguma criatura para a batalha.
	 * 
	 * @param alive
	 *            Se a criatura esta viva ou nao.
	 * @param creatureIndex
	 *            Index da criatura dentro da lista de criaturas.
	 * @param size
	 *            Tamanho da lista de criaturas.
	 * @return True se ainda restam criaturas. False se todas estao mortas.
	 * 
	 */
	private boolean checkCreatureRemaining(int creatureIndex, int size) {
		return creatureIndex < size;

	}

	@Override
	public void draw(float deltaTime) {
		// TODO Auto-generated method stub

	}

}
